import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_test_demo/pages/search/NetworkPage.dart';
import 'package:flutter_test_demo/static/colors/MyColors.dart';
import 'package:toast/toast.dart';

class SearchPage extends StatefulWidget {
  SearchPage({Key key}) : super(key: key);

  @override
  _SearchPageState createState() => _SearchPageState();
}

class _SearchPageState extends State<SearchPage> {
  TextEditingController _textControler = TextEditingController();

  List hotName = ["壁纸","图腾","娱乐","体育", "海关总署","十九大 理论新视野"];
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("搜索"),
          backgroundColor: MyColor.titleColor,
        ),
        body: DecoratedBox(
          decoration: BoxDecoration(
            color: Color(0xFFFFFFFF)
          ),
          child: Column(
            children: <Widget>[
              Padding(
                padding: EdgeInsets.symmetric(vertical: 10.0, horizontal: 15.0),
                child: Row(
                  children: <Widget>[
                    Expanded(
                      child: TextField(
                        controller: _textControler,
                      ),
                    ),
                    FlatButton(
                      onPressed: () {
                        print(_textControler.text);
                        if(_textControler.text == ""){
                          Toast.show("搜索不能为空", context);
                          return null;
                        }
                        jumpKeyWord(context,_textControler.text);
                      },
                      child: Text(
                        "搜索",
                        style: TextStyle(
                          fontSize: 18.0,
                          height: 2
                        ),
                      ),
                    ),
                  ]
                )
              ),
              DecoratedBox(
                decoration: BoxDecoration(
                  color: Color(0xFFEEEEEE),
                ),

                child: ConstrainedBox(
                  constraints: BoxConstraints.tightFor(
                    height: 15.0,
                    width: double.infinity
                  )
                ),
              ),
              // 热门推荐标题
              Padding(
                padding: EdgeInsets.symmetric(
                  vertical: 20.0,
                  horizontal: 12.0
                ),
                child: Row(
                  children: <Widget>[
                    DecoratedBox(
                      decoration: BoxDecoration(
                        color: MyColor.titleColor,
                      ),
                      child: ConstrainedBox(
                        constraints: BoxConstraints.tightFor(
                          width: 4.0,
                          height: 20.0
                        ),
                      ),
                    ),
                    Padding(
                      padding: EdgeInsets.only(left: 15.0),
                      child: Text(
                        "热门搜索",
                        style: TextStyle(
                          color: MyColor.titleColor,
                          fontSize: 18.0
                        ),
                      ),
                    ),
                  ],
                ),
              ),
              // 热门搜索 内容
              Wrap(
                alignment: WrapAlignment.start,
                spacing: 20.0,
                runSpacing: 15.0,
                children: hotName.map((e) => 
                  TagInfo(keyword: e,)
                ).toList(),
              )



            ]
          ),
        )
      );
  }
}

void jumpKeyWord(BuildContext context,String str){
  Navigator.push(
    context, 
    MaterialPageRoute(builder: (context) => NetWorkPage(str: str),)
  );
}

// 标签
class TagInfo extends StatefulWidget {
  final String keyword;
  TagInfo({Key key , @required this.keyword}) : super(key: key);

  @override
  _TagInfoState createState() => _TagInfoState();
}

class _TagInfoState extends State<TagInfo> {
  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: (){
        jumpKeyWord(context,widget.keyword);
      },
      child: Container(
        decoration: BoxDecoration(
          border: Border.all(color:MyColor.titleColor,width: 1.0),
          borderRadius:BorderRadius.all(Radius.circular(5.0))
        ),
        padding: EdgeInsets.symmetric(vertical: 10.0,horizontal: 13.0),
        child: Text(
          widget.keyword,
          style: TextStyle(
            color: MyColor.titleColor
          ),
        ),
      ),
    );
  }
}